package com.vinaprime.primeschedule.databaselayer;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.vinaprime.primeschedule.entities.VPGroupLine;

/** * 
 * @author LONGPN
 * @since 17/06/2013
 *
 */
@SuppressLint("NewApi")
public class VPGroupLine_DL {
	//Khai bao cau truc cua table, tuong duong voi cac table o database
	private static final String DB_TABLE_NAME = "VP_GROUP_LINE";
	private static final String DB_TABLE_VPGL_ID = "VPGL_ID";
	private static final String DB_TABLE_VPGH_ID = "VPGH_ID";
	private static final String DB_TABLE_VPC_ID = "VPC_ID";
	private static final String DB_TABLE_VPGL_DESCRIPTION = "VPGL_DESCRIPTION";
	private static final String DB_TABLE_VPGL_ENABLE_FLAG  ="VPGL_ENABLE_FLAG";
	public DatabaseController_DL myDatabaseControllerDL;
	public VPGroupLine_DL (Context context, String path)
	{
		this.myDatabaseControllerDL = new DatabaseController_DL(context, path);
	}
	//Get, seter 
	public DatabaseController_DL getMyDatabaseControllerDL() {
		return myDatabaseControllerDL;
	}
	public void setMyDatabaseControllerDL(
			DatabaseController_DL myDatabaseControllerDL) {
		this.myDatabaseControllerDL = myDatabaseControllerDL;
	}
	public  static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	public static String getDbTableVpglId() {
		return DB_TABLE_VPGL_ID;
	}
	public static String getDbTableVpghId() {
		return DB_TABLE_VPGH_ID;
	}
	public static String getDbTableVpcId() {
		return DB_TABLE_VPC_ID;
	}
	public static String getDbTableVpglDescription() {
		return DB_TABLE_VPGL_DESCRIPTION;
	}
	public static String getDbTableVpglEnableFlag() {
		return DB_TABLE_VPGL_ENABLE_FLAG;
	}
	
	//Cac phuong thuc lien quan
	public Cursor getAllGroupLine()
	{
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		String strSelect = "SELECT * FROM "+ DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	}
	
	public Cursor getNewGroupLineID()
	{
		Cursor cur  = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX ("+DB_TABLE_VPGL_ID+") AS " +DB_TABLE_VPGL_ID+ " FROM "+ DB_TABLE_NAME;
		cur  = db.rawQuery(strQuery, null);
		return cur;
	}
	public Cursor getGroupLinebyId(int Id)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPGL_ID +" = " + Id ;
		cur = db.rawQuery(strQuery, null);
		return cur;
	}
	//getGroupLineList
	
	public Cursor getGroupLineList(int headerId)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPGH_ID +" = " + headerId ;
		cur = db.rawQuery(strQuery, null);
		return cur;
	}
	public void AddGroupLine (VPGroupLine _GroupLine)	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPGL_ID, _GroupLine.getVpglID());
		values.put(DB_TABLE_VPGH_ID, _GroupLine.getVpghID());
		values.put(DB_TABLE_VPC_ID, _GroupLine.getmContact().getVpcID());
		values.put(DB_TABLE_VPGL_DESCRIPTION,  _GroupLine.getVpglDescription());
		values.put(DB_TABLE_VPGL_ENABLE_FLAG,  _GroupLine.getVpglEnableFlag());
		db.insert(DB_TABLE_NAME, null, values);		
		db.close();
	}
	public int updateGroupLine (VPGroupLine _GroupLine)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL .getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPGL_ID, _GroupLine.getVpglID());
		values.put(DB_TABLE_VPGH_ID, _GroupLine.getVpghID());
		values.put(DB_TABLE_VPC_ID, _GroupLine.getmContact().getVpcID());
		values.put(DB_TABLE_VPGL_DESCRIPTION,  _GroupLine.getVpglDescription());
		values.put(DB_TABLE_VPGL_ENABLE_FLAG,  _GroupLine.getVpglEnableFlag());
		return db.update(DB_TABLE_NAME, values, DB_TABLE_VPGL_ID + "= ?", new String[] {String.valueOf(_GroupLine.getVpglID())});
		
	}
	
	public void deleteGroupLine (VPGroupLine _GroupLine)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPGL_ID + "= ?", new String[] { String.valueOf(_GroupLine.getVpglID()) });
		
		db.close();
	}
	public void deleteGroupLinebyID(int _VpglID){
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPGL_ID + "= ?",new String[]{ String.valueOf(_VpglID)});
		db.close();
	}

	
}
